﻿#include "printreport.h"
#include<QtPrintSupport/QPrinter>
#include<QPainter>
#include<QString>
#include<Report/smoothcurvegenerator.h>
#include "QJsonArray"
#include "qjsonobject.h"
#include "QJsonDocument"
#include<math.h>

#include "DataPro/datapro.h"
#include"DataPro/listoperation.h"
#include "qmath.h"
#include "QSettings"
#include "QFile"
#include "individualreport.h"
// using namespace stdlib;

PrintReport::PrintReport(DATAINFO_TABLE_DATA data)
{
    //A4大小1150*1640   595 * 842
    DataPro dataPro;
    this->dib=data.dib;
    QString type=dib.TaskType;
    QString reportName=data.TaskID+"_"+data.MeasureTime;
    reportName.replace(":","");
    printer=new QPrinter;

    #ifdef X86
        printer->setPageSize(QPageSize::A4);
    #elif defined ARM
        printer->setPageSize(QPrinter::A4);
    #endif
    printer->setOutputFormat(QPrinter::PdfFormat);


    printer->setResolution(144);
    printer->setPageMargins(QMarginsF(0, 0, 0, 0));
    ParentFolder=BaseFolder+"/"+reportName;
    fo.isDirExist(ParentFolder);
    printer->setOutputFileName(ParentFolder+"/"+dib.TaskType+".pdf");
    painter=new QPainter(printer);
    painter->setRenderHint(QPainter::Antialiasing);

    font_Header.setPixelSize(32);
    font_Header.setFamily("华文宋体");
    font_Header.setBold(true);

    font_DataInfo.setPixelSize(25);
    font_DataInfo.setFamily("华文宋体");
    font_DataInfo.setBold(true);

    font_DataInfo_v.setPixelSize(23);
    font_DataInfo_v.setFamily("华文宋体");
    font_res.setPixelSize(20);
    font_res.setFamily("华文宋体");

    font_subTitle.setPixelSize(25);
    font_subTitle.setFamily("华文宋体");
    font_subTitle.setBold(true);
    QSettings settingsread("Option.ini",QSettings::IniFormat);
    DWName = settingsread.value("DevOption/Name").toString();
    if(type=="团体模式")
    {
        isGroupOrBattle=true;
        this->dataBean=dataPro.JsonObject2GroupDataBean(data);
        Report_group();
    }else
    {
        isGroupOrBattle=false;
        this->battledataBean=dataPro.JsonObject2BattleDataBean(data);
        Report_battle();
    }


    if(dib.TaskType=="团体模式"){
        int len=dataBean.dataBeans.size();
        for(int i=0;i<len;i++)
        {
            IndividualReport w;
            w.setV(dataBean.dataBeans.at(i));
            QPrinter printer;
            fo.isDirExist(ParentFolder+"/个体报告");
            QString fileName = ParentFolder+"/个体报告/"+dataBean.dataBeans.at(i).UserID+"_"+dataBean.dataBeans.at(i).UserName+".pdf";
            if (!fileName.isEmpty()) {
                printer.setOutputFileName(fileName);
                printer.setResolution(300);//值越大分辨率越高
                printer.setPageMargins(QMarginsF(0, 0, 0, 0));

                QPixmap pixmap=w.grab(w.rect()); //抓取界面widget区域，可以抓取任意控件区域
                QPainter painter;
                painter.begin(&printer);
                painter.drawPixmap(0,0,pixmap); //按照坐标画图
                #ifdef X86
                    printer.setPageSize(QPageSize::A4);
                #elif defined ARM
                    printer.setPageSize(QPrinter::A4);
                #endif
            }
        }
    }else
    {
        int len=battledataBean.dataBeans.size();
        for(int i=0;i<len;i++)
        {
            IndividualReport w;
            w.setV(battledataBean.dataBeans.at(i));
            QPrinter printer;
            fo.isDirExist(ParentFolder+"/个体报告");
            QString fileName = ParentFolder+"/个体报告/"+battledataBean.dataBeans.at(i).UserID+"_"+battledataBean.dataBeans.at(i).UserName+".pdf";
                                                                                                           if (!fileName.isEmpty()) {
                printer.setOutputFileName(fileName);
                printer.setResolution(300);//值越大分辨率越高
                printer.setPageMargins(QMarginsF(0, 0, 0, 0));

                QPixmap pixmap=w.grab(w.rect()); //抓取界面widget区域，可以抓取任意控件区域
                QPainter painter;
                painter.begin(&printer);
                painter.drawPixmap(0,0,pixmap); //按照坐标画图
                #ifdef X86
                    printer.setPageSize(QPageSize::A4);
                #elif defined ARM
                    printer.setPageSize(QPrinter::A4);
                #endif
            }
        }
    }
}

PrintReport::~PrintReport()
{
    delete painter;

}

void PrintReport::DrawTitle(int startX,int startY,QString title)
{
    painter->setFont(font_Header);
    painter->drawText(QRect(startX,startY,800,50),title);
}
void PrintReport::DrawsubTitle(int startX,int startY,QString title,int width)
{
    painter->setBrush(QBrush(QColor(220,220,220),Qt::SolidPattern));
    painter->drawRect(QRect(QPoint(startX,startY),QPoint(startX+width,startY+40)));
    painter->setFont(font_subTitle);
    painter->drawText(QRect(startX+20,startY+5,width,40),title);
}
void PrintReport::DrawDataInfo1(int startX,int startY)
{
    painter->setFont(font_DataInfo);
    int titleWidth=140;
    int titleHeight=30;
    painter->drawText(QRect(startX,startY,titleWidth,titleHeight) ,"任务名称：");
//    painter->drawText(QRect(startX+380,startY,titleWidth,titleHeight),"模式类别：");
//    painter->drawText(QRect(startX+760,startY,titleWidth,titleHeight),"训练主题：");

    painter->drawLine(QPoint(startX+titleWidth,startY+titleHeight), QPoint(startX+titleWidth+1000,startY+titleHeight));
//    painter->drawLine(QPoint(startX+380+titleWidth,startY+titleHeight),QPoint(startX+620+titleWidth,startY+titleHeight));
//    painter->drawLine(QPoint(startX+760+titleWidth,startY+titleHeight),QPoint(startX+1000+titleWidth,startY+titleHeight));

    painter->setFont(font_DataInfo_v);
    painter->drawText(QRect(startX+titleWidth,startY,850,titleHeight),dib.TaskID);
//    painter->drawText(QRect(startX+380+titleWidth,startY,250,titleHeight),dib.TaskType);
//    painter->drawText(QRect(startX+760+titleWidth,startY,250,titleHeight),dib.Theme);

}

void PrintReport::DrawDataInfo2(int startX,int startY)
{
    painter->setFont(font_DataInfo);
    int titleWidth=140;
    int titleHeight=30;
    painter->drawText(QRect(startX,startY,titleWidth,titleHeight) ,"训练内容：");
    painter->drawText(QRect(startX+380,startY,titleWidth,titleHeight),"团体人数：");
    painter->drawText(QRect(startX+760,startY,titleWidth,titleHeight),"训练用时：");

    painter->drawLine(QPoint(startX+titleWidth,startY+titleHeight), QPoint(startX+titleWidth+230,startY+titleHeight));
    painter->drawLine(QPoint(startX+380+titleWidth,startY+titleHeight),QPoint(startX+620+titleWidth,startY+titleHeight));
    painter->drawLine(QPoint(startX+760+titleWidth,startY+titleHeight),QPoint(startX+1000+titleWidth,startY+titleHeight));

    painter->setFont(font_DataInfo_v);
    painter->drawText(QRect(startX+titleWidth,startY,250,titleHeight),dib.Content);
    if(isGroupOrBattle){
        painter->drawText(QRect(startX+380+titleWidth,startY,250,titleHeight),dataBean.Group_Number);
        painter->drawText(QRect(startX+760+titleWidth,startY,250,titleHeight),dataBean.UseTime+"分钟");
    }else
    {
        painter->drawText(QRect(startX+380+titleWidth,startY,250,titleHeight),battledataBean.Group_Number);
        painter->drawText(QRect(startX+760+titleWidth,startY,250,titleHeight),battledataBean.UseTime+"分钟");
    }

}

void PrintReport::DrawDataInfo3(int startX,int startY)
{
    painter->setFont(font_DataInfo);
    int titleWidth=140;
    int titleHeight=30;
    painter->drawText(QRect(startX,startY,titleWidth,titleHeight) ,"训练主题：");
    painter->drawText(QRect(startX+380,startY,titleWidth,titleHeight),"模式类别：");
    painter->drawText(QRect(startX+760,startY,titleWidth,titleHeight),"训练时间：");

    painter->drawLine(QPoint(startX+titleWidth,startY+titleHeight), QPoint(startX+titleWidth+230,startY+titleHeight));
    painter->drawLine(QPoint(startX+380+titleWidth,startY+titleHeight),QPoint(startX+620+titleWidth,startY+titleHeight));
    painter->drawLine(QPoint(startX+760+titleWidth,startY+titleHeight),QPoint(startX+1000+titleWidth,startY+titleHeight));

    painter->setFont(font_DataInfo_v);
    if(isGroupOrBattle){
        painter->drawText(QRect(startX+titleWidth,startY,250,titleHeight),dib.Theme);
        painter->drawText(QRect(startX+380+titleWidth,startY,250,titleHeight),dib.TaskType);
        painter->drawText(QRect(startX+760+titleWidth,startY,250,titleHeight),dib.MeasureTime);
    }else
    {
        painter->drawText(QRect(startX+titleWidth,startY,250,titleHeight),battledataBean.TaskTheme);
        painter->drawText(QRect(startX+380+titleWidth,startY,250,titleHeight),dib.TaskType);
        painter->drawText(QRect(startX+760+titleWidth,startY,250,titleHeight),dib.MeasureTime);
    }

}

void PrintReport::DrawResultTitle(int startX,int startY)
{
    painter->setFont(font_DataInfo);
    int titleWidth=140;
    int titleHeight=30;
    painter->drawText(QRect(startX,startY,titleWidth,titleHeight) ,"数据类型");
    painter->drawText(QRect(startX+300,startY,titleWidth,titleHeight),"平均值");
    painter->drawText(QRect(startX+600,startY,titleWidth,titleHeight),"参考范围");
    painter->drawText(QRect(startX+900,startY,titleWidth,titleHeight),"数据结果");

}

void PrintReport::DrawResultV(int startX,int startY)
{
    painter->setFont(font_DataInfo_v);
    int titleWidth=140;
    int titleHeight=25;

    //精神压力指数
    painter->drawText(QRect(startX,startY,titleWidth,titleHeight) ,"精神压力指数");
    double mpv=lo.ListAverage(dataBean.MentalPressureIndexs);
    painter->drawText(QRect(startX+330,startY,titleWidth,titleHeight),QString::number(mpv,'f',1));
    painter->drawText(QRect(startX+630,startY,titleWidth,titleHeight),"0-70");
    if(mpv>70)
        painter->drawText(QRect(startX+930,startY,titleWidth,titleHeight),"偏高");
    else if(mpv<30)
        painter->drawText(QRect(startX+930,startY,titleWidth,titleHeight),"极小");
    else
        painter->drawText(QRect(startX+930,startY,titleWidth,titleHeight),"正常");

    //抗压能力指数
    painter->drawText(QRect(startX,startY+40,titleWidth,titleHeight) ,"抗压能力指数");
    double rmpv=lo.ListAverage(dataBean.ReMental);
    painter->drawText(QRect(startX+330,startY+40,titleWidth,titleHeight),QString::number(rmpv,'f',1));
    painter->drawText(QRect(startX+630,startY+40,titleWidth,titleHeight),"30-100");
    if(rmpv<30)
        painter->drawText(QRect(startX+930,startY+40,titleWidth,titleHeight),"偏低");
    else if(rmpv>70)
        painter->drawText(QRect(startX+930,startY+40,titleWidth,titleHeight),"超强");
    else
        painter->drawText(QRect(startX+930,startY+40,titleWidth,titleHeight),"正常");

    //疲劳指数
    painter->drawText(QRect(startX,startY+80,titleWidth,titleHeight) ,"身体疲劳指数");
    double fatigure=lo.ListAverage(dataBean.Fatigure);
    painter->drawText(QRect(startX+330,startY+80,titleWidth,titleHeight),QString::number(fatigure,'f',1));
    painter->drawText(QRect(startX+630,startY+80,titleWidth,titleHeight),"0-70");
    if(fatigure>70)
        painter->drawText(QRect(startX+930,startY+80,titleWidth,titleHeight),"偏高");
    else if(fatigure<30)
        painter->drawText(QRect(startX+930,startY+80,titleWidth,titleHeight),"极小");
    else
        painter->drawText(QRect(startX+930,startY+80,titleWidth,titleHeight),"正常");

    //情绪消极指数
    painter->drawText(QRect(startX,startY+120,titleWidth,titleHeight) ,"情绪消极指数");
    double depress=lo.ListAverage(dataBean.Depress);
    painter->drawText(QRect(startX+330,startY+120,titleWidth,titleHeight),QString::number(depress,'f',1));
    painter->drawText(QRect(startX+630,startY+120,titleWidth,titleHeight),"0-70");
    if(depress>70)
        painter->drawText(QRect(startX+930,startY+120,titleWidth,titleHeight),"消极");
    else if(depress<30)
        painter->drawText(QRect(startX+930,startY+120,titleWidth,titleHeight),"正常");
    else
        painter->drawText(QRect(startX+930,startY+120,titleWidth,titleHeight),"正常");

    //情绪焦虑指数
    painter->drawText(QRect(startX,startY+160,titleWidth,titleHeight) ,"情绪焦虑指数");
    double ansiety=lo.ListAverage(dataBean.Ansiety);
    painter->drawText(QRect(startX+330,startY+160,titleWidth,titleHeight),QString::number(ansiety,'f',1));
    painter->drawText(QRect(startX+630,startY+160,titleWidth,titleHeight),"0-70");
    if(ansiety>70)
        painter->drawText(QRect(startX+930,startY+160,titleWidth,titleHeight),"焦虑");
    else if(ansiety<30)
        painter->drawText(QRect(startX+930,startY+160,titleWidth,titleHeight),"正常");
    else
        painter->drawText(QRect(startX+930,startY+160,titleWidth,titleHeight),"正常");

    //自主神经系统平衡性
    painter->drawText(QRect(startX,startY+200,titleWidth+30,titleHeight) ,"自主神经平衡性");
    double ballance=lo.ListAverage(dataBean.Ballance);
    painter->drawText(QRect(startX+330,startY+200,titleWidth,titleHeight),QString::number(ballance,'f',1));
    painter->drawText(QRect(startX+630,startY+200,titleWidth,titleHeight),"0-70");
    if(ballance>70)
        painter->drawText(QRect(startX+930,startY+200,titleWidth,titleHeight),"极好");
    else if(ballance<30)
        painter->drawText(QRect(startX+930,startY+200,titleWidth,titleHeight),"极差");
    else
        painter->drawText(QRect(startX+930,startY+200,titleWidth,titleHeight),"正常");


    //血压变异指数
    painter->drawText(QRect(startX,startY+240,titleWidth,titleHeight) ,"血压变异指数");
    double sdp=lo.ListAverage(dataBean.SDP);
    painter->drawText(QRect(startX+330,startY+240,titleWidth,titleHeight),QString::number(sdp,'f',1));
    painter->drawText(QRect(startX+630,startY+240,titleWidth,titleHeight),"110-130");
    if(sdp>130)
        painter->drawText(QRect(startX+930,startY+240,titleWidth,titleHeight),"偏高");
    else if(sdp<110)
        painter->drawText(QRect(startX+930,startY+240,titleWidth,titleHeight),"偏小");
    else
        painter->drawText(QRect(startX+930,startY+240,titleWidth,titleHeight),"正常");

    //身心调控综合指数
    painter->drawText(QRect(startX,startY+280,titleWidth,titleHeight) ,"身心综合指数");

    double score=lo.ListAverage(dataBean.PhyScore);;
    painter->drawText(QRect(startX+330,startY+280,titleWidth,titleHeight),QString::number(score,'f',1));
    painter->drawText(QRect(startX+630,startY+280,titleWidth,titleHeight),"30-100");
    if(score<30)
        painter->drawText(QRect(startX+930,startY+280,titleWidth,titleHeight),"极差");
    else if(sdp>70)
        painter->drawText(QRect(startX+930,startY+280,titleWidth,titleHeight),"极好");
    else
        painter->drawText(QRect(startX+930,startY+280,titleWidth,titleHeight),"正常");


}
void PrintReport::DrawResultV_battle(int startX,int startY)
{
    painter->setFont(font_DataInfo_v);
    int titleWidth=140;
    int titleHeight=25;

    //精神压力指数
    painter->drawText(QRect(startX,startY,titleWidth,titleHeight) ,"精神压力指数");
    double mpv=lo.ListAverage(battledataBean.MentalPressureIndexs);
    painter->drawText(QRect(startX+330,startY,titleWidth,titleHeight),QString::number(mpv,'f',1));
    painter->drawText(QRect(startX+630,startY,titleWidth,titleHeight),"0-70");
    if(mpv>70)
        painter->drawText(QRect(startX+930,startY,titleWidth,titleHeight),"偏高");
    else if(mpv<30)
        painter->drawText(QRect(startX+930,startY,titleWidth,titleHeight),"极小");
    else
        painter->drawText(QRect(startX+930,startY,titleWidth,titleHeight),"正常");

    //抗压能力指数
    painter->drawText(QRect(startX,startY+40,titleWidth,titleHeight) ,"抗压能力指数");
    double rmpv=lo.ListAverage(battledataBean.ReMental);
    painter->drawText(QRect(startX+330,startY+40,titleWidth,titleHeight),QString::number(rmpv,'f',1));
    painter->drawText(QRect(startX+630,startY+40,titleWidth,titleHeight),"30-100");
    if(rmpv<30)
        painter->drawText(QRect(startX+930,startY+40,titleWidth,titleHeight),"偏低");
    else if(rmpv>70)
        painter->drawText(QRect(startX+930,startY+40,titleWidth,titleHeight),"超强");
    else
        painter->drawText(QRect(startX+930,startY+40,titleWidth,titleHeight),"正常");

    //疲劳指数
    painter->drawText(QRect(startX,startY+80,titleWidth,titleHeight) ,"身体疲劳指数");
    double fatigure=lo.ListAverage(battledataBean.Fatigure);
    painter->drawText(QRect(startX+330,startY+80,titleWidth,titleHeight),QString::number(fatigure,'f',1));
    painter->drawText(QRect(startX+630,startY+80,titleWidth,titleHeight),"0-70");
    if(fatigure>70)
        painter->drawText(QRect(startX+930,startY+80,titleWidth,titleHeight),"偏高");
    else if(fatigure<30)
        painter->drawText(QRect(startX+930,startY+80,titleWidth,titleHeight),"极小");
    else
        painter->drawText(QRect(startX+930,startY+80,titleWidth,titleHeight),"正常");

    //情绪消极指数
    painter->drawText(QRect(startX,startY+120,titleWidth,titleHeight) ,"情绪消极指数");
    double depress=lo.ListAverage(battledataBean.Depress);
    painter->drawText(QRect(startX+330,startY+120,titleWidth,titleHeight),QString::number(depress,'f',1));
    painter->drawText(QRect(startX+630,startY+120,titleWidth,titleHeight),"0-70");
    if(depress>70)
        painter->drawText(QRect(startX+930,startY+120,titleWidth,titleHeight),"消极");
    else if(depress<30)
        painter->drawText(QRect(startX+930,startY+120,titleWidth,titleHeight),"正常");
    else
        painter->drawText(QRect(startX+930,startY+120,titleWidth,titleHeight),"正常");

    //情绪焦虑指数
    painter->drawText(QRect(startX,startY+160,titleWidth,titleHeight) ,"情绪焦虑指数");
    double ansiety=lo.ListAverage(battledataBean.Ansiety);
    painter->drawText(QRect(startX+330,startY+160,titleWidth,titleHeight),QString::number(ansiety,'f',1));
    painter->drawText(QRect(startX+630,startY+160,titleWidth,titleHeight),"0-70");
    if(ansiety>70)
        painter->drawText(QRect(startX+930,startY+160,titleWidth,titleHeight),"焦虑");
    else if(ansiety<30)
        painter->drawText(QRect(startX+930,startY+160,titleWidth,titleHeight),"正常");
    else
        painter->drawText(QRect(startX+930,startY+160,titleWidth,titleHeight),"正常");

    //自主神经系统平衡性
    painter->drawText(QRect(startX,startY+200,titleWidth+30,titleHeight) ,"自主神经平衡性");
    double ballance=lo.ListAverage(battledataBean.Ballance);
    painter->drawText(QRect(startX+330,startY+200,titleWidth,titleHeight),QString::number(ballance,'f',1));
    painter->drawText(QRect(startX+630,startY+200,titleWidth,titleHeight),"0-70");
    if(ballance>70)
        painter->drawText(QRect(startX+930,startY+200,titleWidth,titleHeight),"极好");
    else if(ballance<30)
        painter->drawText(QRect(startX+930,startY+200,titleWidth,titleHeight),"极差");
    else
        painter->drawText(QRect(startX+930,startY+200,titleWidth,titleHeight),"正常");


    //血压变异指数
    painter->drawText(QRect(startX,startY+240,titleWidth,titleHeight) ,"血压变异指数");
    double sdp=lo.ListAverage(battledataBean.SDP);
    painter->drawText(QRect(startX+330,startY+240,titleWidth,titleHeight),QString::number(sdp,'f',1));
    painter->drawText(QRect(startX+630,startY+240,titleWidth,titleHeight),"110-130");
    if(sdp>130)
        painter->drawText(QRect(startX+930,startY+240,titleWidth,titleHeight),"偏高");
    else if(sdp<110)
        painter->drawText(QRect(startX+930,startY+240,titleWidth,titleHeight),"偏小");
    else
        painter->drawText(QRect(startX+930,startY+240,titleWidth,titleHeight),"正常");

    //身心调控综合指数
    painter->drawText(QRect(startX,startY+280,titleWidth,titleHeight) ,"身心综合指数");

    double score=lo.ListAverage(battledataBean.PhyScore);
    painter->drawText(QRect(startX+330,startY+280,titleWidth,titleHeight),QString::number(score,'f',1));
    painter->drawText(QRect(startX+630,startY+280,titleWidth,titleHeight),"30-100");
    if(score<30)
        painter->drawText(QRect(startX+930,startY+280,titleWidth,titleHeight),"极差");
    else if(sdp>70)
        painter->drawText(QRect(startX+930,startY+280,titleWidth,titleHeight),"极好");
    else
        painter->drawText(QRect(startX+930,startY+280,titleWidth,titleHeight),"正常");


}
void PrintReport::DrawScoreTitle(int startX,int startY)
{
    painter->setFont(font_DataInfo);
    int titleWidth=140;
    int titleHeight=30;
    painter->drawText(QRect(startX,startY,titleWidth,titleHeight) ,"名次");
    painter->drawText(QRect(startX+300,startY,titleWidth,titleHeight),"编号");
    painter->drawText(QRect(startX+600,startY,titleWidth,titleHeight),"姓名");
    painter->drawText(QRect(startX+900,startY,titleWidth,titleHeight),"身心指数");

}
void PrintReport::DrawScoreTitle_battle(int startX,int startY)
{
    painter->setFont(font_DataInfo);
    int titleWidth=140;
    int titleHeight=30;
    painter->drawText(QRect(startX,startY,titleWidth,titleHeight) ,"名次");
    painter->drawText(QRect(startX+300,startY,titleWidth,titleHeight),"组别");
    painter->drawText(QRect(startX+600,startY,titleWidth,titleHeight),"前三名");
    painter->drawText(QRect(startX+900,startY,titleWidth,titleHeight),"身心指数");
}
void PrintReport::DrawScores(int mc,int startX,int startY,ReportDataForSort pdfs)
{
    painter->setFont(font_DataInfo_v);
    int titleWidth=140;
    int titleHeight=25;
    painter->drawText(QRect(startX+20,startY,titleWidth,titleHeight) ,QString::number(mc));
    painter->drawText(QRect(startX+300,startY,titleWidth,titleHeight),pdfs.id);
    painter->drawText(QRect(startX+600,startY,titleWidth,titleHeight),pdfs.name);
    painter->drawText(QRect(startX+920,startY,titleWidth,titleHeight),QString::number(pdfs.score,'f',1));

}
void PrintReport::DrawAllScore()
{
    int num=dataBean.Group_Number.toInt();
    QList<ReportDataForSort> m_data;
    QList<double>scores;
    ReportDataForSort rdfs;
    for (int i = 0; i < num; ++i) {
        double score=dataBean.PhyScore.at(i);
        rdfs.id=dataBean.UserID.at(i);
        rdfs.name=dataBean.UserNames.at(i);
        rdfs.score=score;
        m_data.append(rdfs);
    }

    std::sort(m_data.begin(),m_data.end(),compareData);
    // qSort
    int loopNum=num>=3?3:num;
    for(int i=0;i<loopNum;i++)
    {
        DrawScores(i+1,90,930+i*40,m_data.at(i));
    }

}

void PrintReport::DrawAllScore_Battle()
{
    int num=battledataBean.GroutEixstNum;
    int t1=num/1000;
    int t2=num%1000/100;
    int t3=num%1000%100/10;
    int t4=num%1000%100%10;
    GroupA_Ex=t1==1;
    GroupB_Ex=t2==1;
    GroupC_Ex=t3==1;
    GroupD_Ex=t4==1;

    QList<ReportDataForSort> m_data;
    QList<double>scores;
    ReportDataForSort rdfs;
    int GroupExistNum=0;
    if(GroupA_Ex)
    {
        GroupExistNum++;
        rdfs.id="A组";
        rdfs.name=battledataBean.Group_A_Names;
        rdfs.score=battledataBean.PhyScore_A.last();;
        m_data.append(rdfs);
    }
    if(GroupB_Ex)
    {
        GroupExistNum++;
        rdfs.id="B组";
        rdfs.name=battledataBean.Group_B_Names;
        rdfs.score=battledataBean.PhyScore_B.last();;
        m_data.append(rdfs);
    }
    if(GroupC_Ex)
    {
        GroupExistNum++;
        rdfs.id="C组";
        rdfs.name=battledataBean.Group_C_Names;
        rdfs.score=battledataBean.PhyScore_C.last();;
        m_data.append(rdfs);
    }

    if(GroupD_Ex)
    {
        GroupExistNum++;
        rdfs.id="D组";
        rdfs.name=battledataBean.Group_D_Names;
        rdfs.score=battledataBean.PhyScore_D.last();;
        m_data.append(rdfs);
    }


    std::sort(m_data.begin(),m_data.end(),compareData);
    int loopNum=GroupExistNum>=3?3:GroupExistNum;
    for(int i=0;i<loopNum;i++)
    {
        DrawScores(i+1,90,930+i*40,m_data.at(i));
    }

}
void PrintReport::DrawLine(int startX,int startY,int width,int height,QList<double>s)
{
    QFont font5("华文宋体",8,false);
    QPen pen_White,pen_Yello,Pen_Gray,Pen_Black;
    pen_White.setWidth(1);
    pen_Yello.setWidth(2);
    Pen_Gray.setWidth(1);
    Pen_Black.setWidth(1);
    pen_White.setColor(QColor(255,255,255));
    pen_Yello.setColor(QColor(255,128,0));
    Pen_Gray.setColor(QColor(220,220,220));
    Pen_Black.setColor(QColor(0,0,0));
    QFont font4("华文宋体",10,false);
    painter->setFont(font5);
    int segNum=5;
    for(int i=0;i<segNum+1;i++){

        if(i>0 && i<segNum)
        {
            painter->setPen(Pen_Gray);
            painter->drawLine(startX,startY+height*i,startX+width,startY+height*i);
        }
        painter->setPen(Pen_Black);
        painter->drawLine(startX,startY+height*i,startX+3,startY+height*i);
        if(i<5){
            painter->drawText(QRect(startX-25,startY+height*i-8,startX,startY+height*i-8),QString::number(100-i*20));
        }else{
            painter->drawText(QRect(startX-20,startY+height*i-8,startX,startY+height*i-8),QString::number(100-i*20));
        }
    }



    painter->setPen(Pen_Gray);
    painter->drawLine(startX+3,startY,startX+3,startY+segNum*height);//左侧y轴
    painter->drawLine(startX+3,startY+segNum*height,startX+width,startY+segNum*height);//下面x轴
    painter->drawLine(startX+3,startY,startX+width,startY);//上面x轴
    painter->drawLine(startX+width,startY,startX+width,startY+segNum*height);//右侧y轴

    painter->setPen(pen_Yello);
    QList<QPointF> points1; // 曲线一的顶点数组
    QPainterPath smoothCurvePath1; // 平滑曲线一
    // 生成曲线的坐标
    double x1=startX+3;
    double y1=0.0;
    for (int i = 0; i < s.size(); i++) {
        x1 += width / s.size();
        y1 = (100-s.at(i))*6*height/100+startY;
        points1 << QPointF(x1, y1);
    }
    // 使用曲线的坐标生成平滑曲线
    smoothCurvePath1 = SmoothCurveGenerator::generateSmoothCurve(points1); // 不封闭
    // 绘制平滑曲线和曲线上的顶点
    painter->setRenderHint(QPainter::Antialiasing);
    painter->setBrush(QBrush(QColor(255,255,255),Qt::SolidPattern));
    painter->setPen(QPen(QColor(255,128,0), 2));
    painter->drawPath(smoothCurvePath1);
    //        painter->setBrush(Qt::gray);
    for (int i = 0; i < points1.length() ; i += 1) {
        painter->drawEllipse(points1[i].x()-1, points1[i].y()-1, 3, 3);
    }
    painter->setPen(Pen_Black);
}

void PrintReport::DrawLines(int startX,int startY,int width,int height,QList<QList<double>>s)
{
    QFont font5("华文宋体",8,false);
    QPen pen_White,pen_Yello,Pen_Gray,Pen_Black;
    pen_White.setWidth(1);
    pen_Yello.setWidth(2);
    Pen_Gray.setWidth(1);
    Pen_Black.setWidth(1);
    pen_White.setColor(QColor(255,255,255));
    pen_Yello.setColor(QColor(255,128,0));
    Pen_Gray.setColor(QColor(220,220,220));
    Pen_Black.setColor(QColor(0,0,0));
    QFont font4("华文宋体",10,false);
    painter->setFont(font5);
    int segNum=5;
    for(int i=0;i<segNum+1;i++){

        if(i>0 && i<segNum)
        {
            painter->setPen(Pen_Gray);
            painter->drawLine(startX,startY+height*i,startX+width,startY+height*i);
        }
        painter->setPen(Pen_Black);
        painter->drawLine(startX,startY+height*i,startX+3,startY+height*i);
        if(i<5){
            painter->drawText(QRect(startX-25,startY+height*i-8,startX,startY+height*i-8),QString::number(100-i*20));
        }else{
            painter->drawText(QRect(startX-20,startY+height*i-8,startX,startY+height*i-8),QString::number(100-i*20));
        }
    }



    painter->setPen(Pen_Gray);
    painter->drawLine(startX+3,startY,startX+3,startY+segNum*height);//左侧y轴
    painter->drawLine(startX+3,startY+segNum*height,startX+width,startY+segNum*height);//下面x轴
    painter->drawLine(startX+3,startY,startX+width,startY);//上面x轴
    painter->drawLine(startX+width,startY,startX+width,startY+segNum*height);//右侧y轴

    painter->setPen(pen_Yello);
    QList<QPointF> pointsA,pointsB,pointsC,pointsD; // 曲线一的顶点数组
    QPainterPath smoothCurvePathA,smoothCurvePathB,smoothCurvePathC,smoothCurvePathD; // 平滑曲线一
    // 生成曲线的坐标
    double x1=startX+3;
    double y1=0.0;

    if(GroupA_Ex){
        for (int i = 0; i < s.at(0).size(); i++) {
            x1 += width / s.at(0).size();
            y1 = (100-s.at(0).at(i))*6*height/100+startY;
            pointsA << QPointF(x1, y1);
        }
        smoothCurvePathA = SmoothCurveGenerator::generateSmoothCurve(pointsA,false); // 不封闭
    }
    if(GroupB_Ex){
        x1=startX+3;
        for (int i = 0; i < s.at(1).size(); i++) {
            x1 += width / s.at(1).size();
            y1 = (100-s.at(1).at(i))*6*height/100+startY;
            pointsB << QPointF(x1, y1);
        }
        smoothCurvePathB = SmoothCurveGenerator::generateSmoothCurve(pointsB,false); // 不封闭
    }
    if(GroupC_Ex){
        x1=startX+3;
        for (int i = 0; i < s.at(2).size(); i++) {
            x1 += width / s.at(2).size();
            y1 = (100-s.at(2).at(i))*6*height/100+startY;
            pointsC << QPointF(x1, y1);
        }
         smoothCurvePathC = SmoothCurveGenerator::generateSmoothCurve(pointsC); // 不封闭
    }
    if(GroupD_Ex){
         x1=startX+3;
        for (int i = 0; i < s.at(3).size(); i++) {
            x1 += width / s.at(3).size();
            y1 = (100-s.at(3).at(i))*6*height/100+startY;
            pointsD << QPointF(x1, y1);
        }
         smoothCurvePathD = SmoothCurveGenerator::generateSmoothCurve(pointsD); // 不封闭, // 使用曲线的坐标生成平滑曲线
    }

    // 绘制平滑曲线和曲线上的顶点
    painter->setRenderHint(QPainter::Antialiasing);
    painter->setBrush(QBrush(QColor(255,255,255,0),Qt::SolidPattern));

    int deltaY=30;
    int startLabelRectAndText=920;
    if(GroupA_Ex){
         painter->setPen(QPen(QColor(00,0xDF,0xF5), 2));
         painter->drawPath(smoothCurvePathA);
         for (int i = 0; i < pointsA.length() ; i += 1) {
            painter->drawEllipse(pointsA[i].x()-1, pointsA[i].y()-1, 3, 3);
         }
         painter->setBrush(QBrush(QColor(00,0xDF,0xF5),Qt::SolidPattern));
         painter->drawRect(QRect(QPoint(startX+startLabelRectAndText,startY+20),QPoint(startX+startLabelRectAndText+20,startY+30)));
         painter->setFont(font_DataInfo_v);
         painter->drawText(QRect(startX+startLabelRectAndText+25,startY+15,startX+startLabelRectAndText+40,startY+88),"A组");
    }

    if(GroupB_Ex){
         painter->setBrush(QBrush(QColor(255,255,255,0),Qt::SolidPattern));
         painter->setPen(QPen(QColor(0xEE,0x44,0xF5), 2));
         painter->drawPath(smoothCurvePathB);
         for (int i = 0; i < pointsB.length() ; i += 1) {
            painter->drawEllipse(pointsB[i].x()-1, pointsB[i].y()-1, 3, 3);
         }
         painter->setBrush(QBrush(QColor(0xEE,0x44,0xF5),Qt::SolidPattern));
         painter->drawRect(QRect(QPoint(startX+startLabelRectAndText,startY+20+deltaY),QPoint(startX+startLabelRectAndText+20,startY+30+deltaY)));
         painter->setFont(font_DataInfo_v);
         painter->drawText(QRect(startX+startLabelRectAndText+25,startY+15+deltaY,startX+startLabelRectAndText+40,startY+48+deltaY),"B组");
    }


    if(GroupC_Ex){
         painter->setBrush(QBrush(QColor(255,255,255,0),Qt::SolidPattern));
         painter->setPen(QPen(QColor(0xD3,0xDF,0x33), 2));
         painter->drawPath(smoothCurvePathC);
         for (int i = 0; i < pointsC.length() ; i += 1) {
            painter->drawEllipse(pointsC[i].x()-1, pointsC[i].y()-1, 3, 3);
         }
         painter->setBrush(QBrush(QColor(0xD3,0xDF,0x33),Qt::SolidPattern));
         painter->drawRect(QRect(QPoint(startX+startLabelRectAndText,startY+20+2*deltaY),QPoint(startX+startLabelRectAndText+20,startY+30+2*deltaY)));
         painter->setFont(font_DataInfo_v);
         painter->drawText(QRect(startX+startLabelRectAndText+25,startY+15+2*deltaY,startX+startLabelRectAndText+40,startY+48+2*deltaY),"C组");
    }


    if(GroupD_Ex){
         painter->setBrush(QBrush(QColor(255,255,255),Qt::SolidPattern));
         painter->setPen(QPen(QColor(0x33,0xA3,0x35), 2));
         painter->drawPath(smoothCurvePathD);
         for (int i = 0; i < pointsD.length() ; i += 1) {
            painter->drawEllipse(pointsD[i].x()-1, pointsD[i].y()-1, 3, 3);
         }
         painter->setBrush(QBrush(QColor(0x33,0xA3,0x35),Qt::SolidPattern));
         painter->drawRect(QRect(QPoint(startX+startLabelRectAndText,startY+20+3*deltaY),QPoint(startX+startLabelRectAndText+20,startY+30+3*deltaY)));
         painter->setFont(font_DataInfo_v);
         painter->drawText(QRect(startX+startLabelRectAndText+25,startY+15+3*deltaY,startX+startLabelRectAndText+40,startY+48+3*deltaY),"D组");
    }

    painter->setPen(Pen_Black);
}

QList<QList<double>> PrintReport::getScores()
{
    QList<QList<double>>s;
    s.append(battledataBean.PhyScore_A);
    s.append(battledataBean.PhyScore_B);
    s.append(battledataBean.PhyScore_C);
    s.append(battledataBean.PhyScore_D);
    return s;
}
void PrintReport::Report_group(){
    //-----------------绘制标题信息-----------------------
    DrawTitle(340,50,DWName);
    DrawTitle(350,100,"J&S团体心理调控综合训练报告");
    //-----------------绘制测试信息-----------------------
    DrawsubTitle(30,170,"---报告基本信息---",300);
    DrawDataInfo1(30,220);
    DrawDataInfo2(30,270);
    DrawDataInfo3(30,320);
    DrawsubTitle(30,370,"---训练结果报告---",300);
    DrawResultTitle(90,440);
    DrawResultV(80,480);
    DrawsubTitle(30,810,"---训练成绩排名（身心指数）---",450);
    DrawScoreTitle(90,880);
    DrawAllScore();
    DrawsubTitle(30,1050,"---身心调控综合指数趋势图---",450);
    DrawLine(80,1120,1000,30,dataBean.PhyScore_A);

    DrawsubTitle(30,1300,"---身心调控综合指数分布图---",450);
    DrawPies(100,1380);
    DrawsubTitle(530,1300,"---指导建议---",300);



}

void PrintReport::Report_battle(){
    //-----------------绘制标题信息-----------------------
    DrawTitle(340,50,DWName);
    DrawTitle(350,100,"J&S团体心理调控综合训练报告");
    //-----------------绘制测试信息-----------------------
    DrawsubTitle(30,170,"---报告基本信息---",300);
    DrawDataInfo1(30,220);
    DrawDataInfo2(30,270);
    DrawDataInfo3(30,320);
    DrawsubTitle(30,370,"---训练结果报告---",300);
    DrawResultTitle(90,440);
    DrawResultV_battle(80,480);
    DrawsubTitle(30,810,"---训练成绩排名（身心指数）---",450);
    DrawScoreTitle_battle(90,880);//标题头
    DrawAllScore_Battle();
    DrawsubTitle(30,1050,"---身心调控综合指数趋势图---",450);
    DrawLines(80,1120,900,30,getScores());

    DrawsubTitle(30,1300,"---身心调控综合指数分布图---",450);
    DrawPies(100,1380);
    DrawsubTitle(530,1300,"---指导建议---",300);

}
void PrintReport::DrawPies(int startX,int startY)
{
    int num=0;
    if(isGroupOrBattle)
        num=dataBean.Group_Number.toInt();
    else
        num=battledataBean.Group_Number.toInt();

    double score1=0;
    double score2=0;
    double score3=0;
    for (int i = 0; i < num; ++i) {
        double score=0;
        if(isGroupOrBattle)
            score=dataBean.PhyScore.at(i);
        else
            score=battledataBean.PhyScore.at(i);
        if(score<30)
            score1++;
        else if(score>70)
            score3++;
        else
            score2++;
    }
    double t1=score1/num*360;
    double t2=score2/num*360;
    double t3=360-(t1+t2);

    QRect topPointRect(QPoint(startX,startY),QPoint(startX+200,startY+200));
    painter->setBrush(QBrush(QColor(0xC4,0x91,0x44),Qt::SolidPattern));
    painter->drawPie(topPointRect,0, t1 * 16);
    painter->drawRect(QRect(QPoint(startX+230,startY+20),QPoint(startX+270,startY+40)));
    painter->setFont(font_DataInfo_v);
    painter->drawText(QRect(startX+275,startY+18,startX+320,startY+70),"较差");

    painter->setBrush(QBrush(QColor(0x32,0x4A,0xD8),Qt::SolidPattern));
    painter->drawPie(topPointRect,t1 * 16, t2 * 16);
    painter->drawRect(QRect(QPoint(startX+230,startY+90),QPoint(startX+270,startY+110)));
    painter->drawText(QRect(startX+275,startY+88,startX+320,startY+140),"正常");

    painter->setBrush(QBrush(QColor(0x14,0x91,0xF),Qt::SolidPattern));
    painter->drawPie(topPointRect,(t1+t2)*16, t3 * 16);
    painter->drawRect(QRect(QPoint(startX+230,startY+160),QPoint(startX+270,startY+180)));
    painter->drawText(QRect(startX+275,startY+158,startX+320,startY+220),"较好");


    //绘制指导建议
    painter->setBrush(QBrush(QColor(255,255,255),Qt::SolidPattern));
    painter->drawRect(QRect(QPoint(520,1380),QPoint(1150,1580)));
    painter->setFont(font_res);
    painter->drawText(QRect(QPoint(520,1380),QPoint(1150,1620)),"本次训练中，团体成员的训练结果如下：\n“较好”："+QString::number(score3)+"名。该部分成员训练效果出色。\n“正常”："+QString::number(score2)+"名。该部分成员训练效果基本合格。\n“较差”："+QString::number(score1)+"名。该部分成员训练效果较差。\n\n 建议“较好”水平的成员坚持训练，以保持最佳身心状态；建议“正常”和“较差”水平的成员增加训练频次。");
}

bool PrintReport:: compareData(const ReportDataForSort &A1,const ReportDataForSort &A2)
{
    return A1.score>A2.score;
}



